package edu.pitt.dbmi.odie.gapp.gwt.client;

import java.util.List;
import java.util.Map;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.TitleOrientation;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.HTMLFlow;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class ODIE_Client implements EntryPoint {
	
	private TitleOrientation titleOrientation = TitleOrientation.LEFT;
	
	public void onModuleLoad() {
	    Log.setUncaughtExceptionHandler();

	    DeferredCommand.addCommand(new Command() {
	      public void execute() {
	        onModuleLoad2();
	      }
	    });
	  }

	public void onModuleLoad2() {
		Log.setCurrentLogLevel(Log.LOG_LEVEL_DEBUG);
		Log.info("Loading ODIE_Client") ;
		Map<String, List<String>> parameters = com.google.gwt.user.client.Window.Location.getParameterMap();
		for (String key : parameters.keySet()) {
			Log.debug(key) ;
		}
		createMainLayout();
	}

	public void createMainLayout() {
	
		VLayout vLayout = new VLayout();
		vLayout.setWidth100();
		vLayout.setHeight100();
		vLayout.setShowEdges(true);
		vLayout.setMembersMargin(5);
		vLayout.setLayoutMargin(10);
	
		vLayout.addMember(new ODIE_BannarPanel());
		vLayout.addMember(new ODIE_TabbedNavigator()) ;
	
		vLayout.draw();
	}
	
	
	private HLayout createBannarLayout() {
		HLayout hLayout = new HLayout() ;
		hLayout.setWidth("95%");
		hLayout.setHeight("94") ;
		hLayout.setShowEdges(false);
		hLayout.setMembersMargin(5);
		hLayout.setLayoutMargin(10);
		
		// ODIE image
        final Img myImage = new Img("odie64.gif", 64, 64);
        myImage.setAppImgDir("bannar/");
        myImage.setLeft(10);
        myImage.setTop(10);
        hLayout.addMember(myImage);
        
        Label bannarLabel = new Label() ;
        bannarLabel.setAlign(Alignment.LEFT);
        bannarLabel.setValign(VerticalAlignment.CENTER) ;
        bannarLabel.setBorder("1px solid #808080");
        bannarLabel.setBackgroundColor("lightblue");
        bannarLabel.setStyleName("bannarText") ;
        String contents = "<b>Ontology Driven Information Extraction</b>";
        bannarLabel.setContents(contents);
        bannarLabel.setWidth("35%") ;
        hLayout.addMember(bannarLabel) ;
        
        HLayout signInLayout = new HLayout() ;
        signInLayout.setWidth("20%") ;
        signInLayout.setHeight("94") ;
        signInLayout.setShowEdges(false);
        signInLayout.setMembersMargin(1);
        signInLayout.setLayoutMargin(0);
        signInLayout.addMember(createSignInButton()) ;
        signInLayout.addMember(new HTMLFlow("|")) ;
        signInLayout.addMember(createHelpButton()) ;
//        signInLayout.addMember(new HTMLFlow("|")) ;
//        signInLayout.addMember(createAboutButton()) ;
        
        hLayout.addMember(signInLayout) ;
        
		return hLayout;
	}

	public void createLayout() {
		HLayout mainLayout = new HLayout();
		mainLayout.setWidth100();
		mainLayout.setHeight100();

		Label navigationLabel = new Label();
		navigationLabel.setContents("Navigation");
		navigationLabel.setAlign(Alignment.CENTER);
		navigationLabel.setOverflow(Overflow.HIDDEN);
		navigationLabel.setWidth("30%");
		navigationLabel.setShowResizeBar(true);
		navigationLabel.setBorder("1px solid blue");

		mainLayout.addMember(navigationLabel);

		VLayout vLayout = new VLayout();
		vLayout.setWidth("70%");

		Label listingLabel = new Label();
		listingLabel.setContents("Listing");
		listingLabel.setAlign(Alignment.CENTER);
		listingLabel.setOverflow(Overflow.HIDDEN);
		listingLabel.setHeight("30%");
		listingLabel.setShowResizeBar(true);
		listingLabel.setBorder("1px solid blue");

		Label detailsLabel = new Label();
		detailsLabel.setContents("Details");
		detailsLabel.setAlign(Alignment.CENTER);
		detailsLabel.setOverflow(Overflow.HIDDEN);
		detailsLabel.setHeight("70%");
		detailsLabel.setBorder("1px solid blue");

		vLayout.addMember(listingLabel);
		vLayout.addMember(detailsLabel);

		mainLayout.addMember(vLayout);
		mainLayout.draw();
	}
	
	private IButton createSignInButton() {
		IButton button = new IButton("Sign In");
		button.setBorder("0px white");
		button.setBackgroundColor("white") ;
		button.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				SC.say("Signing in.");
			}
		});
		return button ;
	}
	
//	private LinkItem  createSignOutButton() {
//		LinkItem button = new LinkItem("Sign Out") ;
//		button.addClickHandler(new ClickHandler() {
//			public void onClick(ClickEvent event) {
//				SC.say("Signing out.");
//			}
//		});
//		return button ;
//	}
	
	private IButton  createHelpButton() {
		IButton button = new IButton("Help");
		button.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				SC.say("Loading Help.");
			}
		});
		return button ;
	}
	
	private IButton  createAboutButton() {
		IButton button = new IButton("About");
		button.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				SC.say("Loading About.");
			}
		});
		return button ;
	}

	private void createHelloButton() {
		IButton button = new IButton("say hello");
		button.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				SC.say("oh Hi there.");
			}
		});
		button.draw();
	}

	private void createLoginScreen() {

		HLayout layout = new HLayout();

		final DynamicForm form = new DynamicForm();
		form.setWidth(250);

		TextItem usernameItem = new TextItem();
		usernameItem.setTitle("Username");
		usernameItem.setRequired(true);
		usernameItem.setDefaultValue("bob");

		TextItem emailItem = new TextItem();
		emailItem.setTitle("Email");
		emailItem.setRequired(true);
		emailItem.setDefaultValue("bob@isomorphic.com");

		TextItem passwordItem = new TextItem();
		passwordItem.setTitle("Password");
		passwordItem.setRequired(true);
		passwordItem.setType("password");

		TextItem password2Item = new TextItem();
		password2Item.setTitle("Password again");
		password2Item.setRequired(true);
		password2Item.setType("password");

		form.setFields(new FormItem[] { usernameItem, emailItem, passwordItem,
				password2Item });

		IButton swapButton = new IButton("Swap titles");
		swapButton.setLeft(300);
		swapButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (form.getTitleOrientation() == TitleOrientation.TOP) {
					titleOrientation = TitleOrientation.LEFT;
				} else {
					titleOrientation = TitleOrientation.TOP;
				}
				form.setTitleOrientation(titleOrientation);
			}
		});

		layout.addMember(form);
		layout.addMember(swapButton);

		layout.draw();
	}

	public void onClick(ClickEvent event) {
		SC.say("oh Hi there.");
	}

}
